1538B - Friends and Candies - CodeForces Solution


greedy math *800

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    count = 0
    n = int(input())
    candies = [int(x) for x in input().split()]
    min = sum(candies) / n
    
    if min % 1 != 0:
        print(-1)
        continue

    for c in candies:
        if c > min:
            count += 1

    print(count)

C++ Code:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};


void solve() {
    ll n;
    cin >> n;
    vector<ll> a(n);

    ll s = 0;
    for (ll i = 0; i < n; ++i) {
        cin >> a[i];
        s += a[i];
    }

    if (s % n != 0) {
        cout << -1 << "\n";
        return;
    }

    ll ans = 0;

    for (auto x: a) {
        ans += (x > s / n);
    }

    cout << ans << "\n";
}


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    ll tst = 1;
    cin >> tst;

    while (tst--) {
        solve();
    }


    return 0;
}


Comments

Submit
0 Comments
More Questions

1717B - Madoka and Underground Competitions
61B - Hard Work
959B - Mahmoud and Ehab and the message
802G - Fake News (easy)
1717C - Madoka and Formal Statement
420A - Start Up
1031A - Golden Plate
1559C - Mocha and Hiking
427B - Prison Transfer
330A - Cakeminator
426A - Sereja and Mugs
363A - Soroban
1585C - Minimize Distance
1506E - Restoring the Permutation
1539A - Contest Start
363D - Renting Bikes
1198D - Rectangle Painting 1
1023B - Pair of Toys
1725A - Accumulation of Dominoes
1675E - Replace With the Previous Minimize
839A - Arya and Bran
16B - Burglar and Matches
1625B - Elementary Particles
1725G - Garage
1725B - Basketball Together
735A - Ostap and Grasshopper
1183B - Equalize Prices
1481A - Space Navigation
1437B - Reverse Binary Strings
1362B - Johnny and His Hobbies